package com.leyou.item.controller;

import com.leyou.item.interfaces.pojo.vo.GoodsPage;
import com.leyou.item.service.GoodsService;
import com.leyoucommon.pojo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

/**
 * 处理商品的相关信息Controller
 * @version 1.0.0
 * @Author yong.Mr
 * @data 2019-8-9
 */
@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/spu")
public class GoodsController {

    /**
     * 注入GoodsService
     */
    @Autowired
    private GoodsService goodsService;

    /**
     * 根据查询条件分页并排序查询商品信息
     * @param key 模糊查询的条件
     * @param saleable 是否上下架
     * @param page 当前页数
     * @param rows 当前页的数据量
     * @return ResponseEntity<PageResult<Goods>>
     */
    @GetMapping("/page")
    public ResponseEntity<PageResult<GoodsPage>> queryBrandByPage(
            @RequestParam(value = "key",required = false) String key,
            @RequestParam(value = "saleable",required = false,defaultValue = "true") Boolean saleable,
            @RequestParam(value = "page",required = false,defaultValue = "1") Integer page,
            @RequestParam(value = "rows",required = false,defaultValue = "5") Integer rows){
        System.out.println("前端分页的条件数据是否传入----->:" + key + "  " + saleable + "  " + page + "  " + rows);

        PageResult<GoodsPage> goodsPagePageResult = goodsService.queryBrandByPage(key, saleable, page, rows);

        // 判断是否查询到了数据
        if(CollectionUtils.isEmpty(goodsPagePageResult.getItems())){
            return ResponseEntity.notFound().build();
        }
        // 返回给前台的数据
        return ResponseEntity.ok(goodsPagePageResult);
    }



}
